package cz.muni.fi.pa165.library.backend;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import javax.validation.constraints.NotNull;

/**
 *
 * @author Filip Ligac
 */
@Entity
public class Borrow implements Serializable {

    public static final int MAXIMUM_BOOKS_COUNT_PER_BORROW = 6;
    private static final long serialVersionUID = 30L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long borrowID;
    @NotNull
    @Lob
    private Reader reader;
    @NotNull
    @OneToMany
    @Lob
    private List<Book> titles;
    @NotNull
    @Temporal(javax.persistence.TemporalType.TIMESTAMP)
    @Lob
    private Date borrowDate;
    @NotNull
    @Temporal(javax.persistence.TemporalType.TIMESTAMP)
    @Lob
    private Date expirationDate;

    public Borrow() {
    }

    public Borrow(Reader reader, List<Book> titles, Date borrowDate, Date expirationDate) {
        this.reader = reader;
        this.titles = titles;
        this.borrowDate = borrowDate;
        this.expirationDate = expirationDate;
    }

    public Long getBorrowID() {
        return borrowID;
    }

    public Reader getReader() {
        return reader;
    }

    public List<Book> getTitles() {
        return titles;
    }

    public Date getBorrowDate() {
        return borrowDate;
    }

    public Date getExpirationDate() {
        return expirationDate;
    }

    public void setBorrowID(Long borrowID) {
        this.borrowID = borrowID;
    }

    public void setReader(Reader reader) {
        this.reader = reader;
    }

    public void setTitles(List<Book> titles) {
        this.titles = titles;
    }

    public void setBorrowDate(Date borrowDate) {
        this.borrowDate = borrowDate;
    }

    public void setExpirationDate(Date expirationDate) {
        this.expirationDate = expirationDate;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (reader != null ? reader.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        if (!(object instanceof Borrow)) {
            return false;
        }
        Borrow other = (Borrow) object;
        if (this.borrowID == null || !this.borrowID.equals(other.getBorrowID())) {
            return false;
        }
        if (this.reader == null || !this.reader.equals(other.getReader())) {
            return false;
        }
        if (titles.size() != other.getTitles().size()) {
            return false;
        }
        for (Book book : titles) {
            if (!other.getTitles().contains(book)) {
                return false;
            }
        }
        if (this.borrowDate == null || !this.borrowDate.equals(other.getBorrowDate())) {
            return false;
        }
        if (this.expirationDate == null || !this.expirationDate.equals(other.getExpirationDate())) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "Borrow[ readerID = " + reader + ", titleID = " + titles + " ]";
    }
}
